package com.yonyou.jrmob.contacts.schedule.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.schedule.vo.ScheduleDetail;

import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 */
public class ScheduleAdapter extends BaseAdapter {

    Context context;
    List<ScheduleDetail> list;

    public ScheduleAdapter(Context context, List<ScheduleDetail> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (null == list) {
            return 0;
        }
        return list.size();
    }

    public void updataList(List<ScheduleDetail> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ScheduleDetail getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewholder holder = null;
        ScheduleDetail vo = list.get(position);
        if (convertView == null) {
            holder = new viewholder();
            convertView = View.inflate(context,
                    R.layout.scheduledetail_item, null);
            holder.point_point = (ImageView) convertView
                    .findViewById(R.id.point_point);
            holder.jiantou = (ImageView) convertView
                    .findViewById(R.id.jiantou);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.item_time = (TextView) convertView
                    .findViewById(R.id.item_time);
            holder.tvType = (TextView) convertView
                    .findViewById(R.id.tv_type_catalog);
            convertView.setTag(holder);
        } else {
            holder = (viewholder) convertView.getTag();
        }

       int section = getSectionForPosition(position);

        if(position == getPositionForSection(section)){
            holder.tvType.setVisibility(View.VISIBLE);
                if ("A".equals(vo.getType())){
                holder.tvType.setText("我发起的");
                }else {
                holder.tvType.setText("我参与的");}

        }else{
            holder.tvType.setVisibility(View.GONE);
        }

        String[] ss = vo.getStartTime().toString().split(" ");
        String[] ampmString = ss[1].split(":");
        int ampmInt = Integer.parseInt(ampmString[0]);
        if (vo.getIsZhongYao().equals("1")) {
            holder.point_point.setImageResource(R.mipmap.redstar);
        }else{
            if (ampmInt<12 ) {
                holder.point_point.setImageResource(R.mipmap.bluepoint);
            }else{
                holder.point_point.setImageResource(R.mipmap.yellowpoint);
            }
        }

        holder.jiantou.setImageResource(R.mipmap.next);
        holder.name.setText(vo.getTheme());
        holder.item_time.setText(ss[0]);

        return convertView;
    }

    public int getSectionForPosition(int position) {
        return list.get(position).getType().charAt(0);
    }


    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getType();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    class viewholder {
        ImageView point_point;
        ImageView jiantou;
        TextView name;
        TextView item_time;
        TextView tvType;
    }
}